//
//  ZFVisitorView.swift
//  WeiBoAPP
//
//  Created by 张学飞 on 2018/12/31.
//  Copyright © 2018 zxf. All rights reserved.
//

import UIKit

class ZFVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame:frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func registAction()  {
        
        print("注册")
        NotificationCenter.default.post(name: Notification.Name(WB_LoginRegist_NNotification), object: nil)
        
        
    }
    
    @objc func loginAction()  {
        print("登录")
        NotificationCenter.default.post(name: Notification.Name(WB_LoginRegist_NNotification), object: nil)
        
    }
    
    // MARK: - 私有控件
    /** 懒加载属性，只有调用UIKit 控件的指定构造函数不需要类型，调用其他都需要使用类型 **/
    private lazy var iconView = UIImageView(image: UIImage(named: "hotNavgation_deselectd"))
    private lazy var iconView2 = UIImageView(image: UIImage(named: "image-1-h"))

    private lazy var lineView:UIView = {
        let  lineView =  UIView.init()
        lineView.backgroundColor = UIColor.lightGray
        return lineView
    }()
    
    private lazy var tipLabel:UILabel = {
        let  label =  UILabel.init()
        label.textAlignment = .center
        label.backgroundColor = UIColor.gray
        label.text = "一些人哈哈哈哈哈哈哈哈哈,一些人哈哈哈哈哈哈哈哈哈"
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var resiginButton:UIButton = {
        
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.setTitle("注册", for: UIControl.State.normal)
        button.backgroundColor = UIColor.red
        button.addTarget(self, action: #selector(registAction), for: UIControl.Event.touchUpInside)
        return button
    }()
    private lazy var loginButton:UIButton = {
        
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.setTitle("登录", for: UIControl.State.normal)
        button.backgroundColor = UIColor.red
        button.addTarget(self, action: #selector(loginAction), for: UIControl.Event.touchUpInside)
        return button
    }()

}


extension ZFVisitorView
{
    
    
    func starAnimation(view:UIView,rose: Bool  ) {
        
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue =  rose ? 2 * M_PI : -2 * M_PI
        ani.repeatCount = MAXFLOAT
        ani.duration = 5
        ani.isRemovedOnCompletion = false
        
        view.layer.add(ani, forKey: nil)
        
    }
    
    func setupUI()  {
        
        backgroundColor = UIColor.white
        addSubview(lineView)
        addSubview(iconView)
        addSubview(iconView2)
        addSubview(tipLabel)
        addSubview(resiginButton)
        addSubview(loginButton)
        
        starAnimation(view: iconView, rose: true)
        starAnimation(view: iconView2, rose: false)
        
        //取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        let margin:CGFloat = 20
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -80))
        
        addConstraint(NSLayoutConstraint(item: iconView2,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView2,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))

//标签
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView2,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 10))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
//注册按钮
        addConstraint(NSLayoutConstraint(item: resiginButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: resiginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: resiginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 60))
        addConstraint(NSLayoutConstraint(item: resiginButton,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 30))
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 60))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 30))
        
        //VFL
        let vicDic = ["lineView":lineView,
                      "resiginButton":resiginButton
            ]
        let metricsDic = ["space":-20,
        ]

        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[lineView]-0-|",
                                                      options: [],
                                                      metrics: nil,
                                                      views: vicDic))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[lineView]-space-[resiginButton]",
                                                      options: [],
                                                      metrics: metricsDic,
                                                      views: vicDic))
        
        
        
        
    }
    
    
    
    
    
}
